/*
  This file is part of 'Trivial chess'.

  'Trivial chess' is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  'Trivial chess' is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with 'Trivial chess'.  If not, see <http://www.gnu.org/licenses/>.
*/

/* SVN FILE: $Id$ */
/**
 * @author         $Author$
 * @version        $Rev$
 * @lastrevision   $Date$
 * @filesource     $URL$
 */

#ifndef SLIDERAFFECTVECABSTRACT_H_
#define SLIDERAFFECTVECABSTRACT_H_

#include <boost/cstdint.hpp>
#include <boost/array.hpp>

#include "AffectAbstract.h"

template<class SliderAffectVec, class Br, class Sq, class Mn, class Vc, typename Int = int8_t>
class SliderAffectVecAbstract
	: public AffectAbstract<SliderAffectVec,Br,Sq,Mn> {

public:
	typedef Sq Square;
	typedef Vc SquareVector;
	typedef typename SquareVector::FileScalar FileScalar;
	typedef typename SquareVector::RankScalar RankScalar;
private:

#	define _(_F,_R) SquareVector(FileScalar(_F),RankScalar(_R))
	static const boost::array<const SquareVector, 4> BISHOP_VECTORS() {
		const boost::array<const SquareVector, 4> ret = {{
				_(1,1),_(1,-1),_(-1,1),_(-1,-1)
			}};
		return ret;
	}
	static const boost::array<const SquareVector, 4> ROOK_VECTORS() {
		const boost::array<const SquareVector, 4> ret = {{
				_(0,1),_(0,-1),_(-1,0),_(1,0)
			}};
		return ret;
	}
	static const boost::array<const SquareVector, 8> QUEEN_VECTORS() {
		const boost::array<const SquareVector, 8> ret = {{
				_(1,1),_(1,-1),_(-1,1),_(-1,-1),
				_(0,1),_(0,-1),_(-1,0),_(1,0)
			}};
		return ret;
	}
#	undef _

public:
	SliderAffectVecAbstract<SliderAffectVec,Br,Sq,Mn,Vc,Int>() : attack(Square::NIL()) {}

	SliderAffectVecAbstract<SliderAffectVec,Br,Sq,Mn,Vc,Int>(const Board& board, const Square sq, const Man mn)
		: board(&board), square(sq), man(mn), attack(sq), i(mn.isQueen()?8:4) {


		this->me().increment();
	}

	void increment() {
		if ( !this->board->isEmpty(this->attack) )
			this->attack = this->square;

		if ( this->attack == this->square ) {
			if (  0 == this->i ) {
				this->i = -1;
				this->attack = Square::NIL();
				return;
			}
			--this->i;
		}

		const SquareVector v = man.isRook() ? this->ROOK_VECTORS()[this->i] :
					(man.isBishop() ? this->BISHOP_VECTORS()[this->i] : this->QUEEN_VECTORS()[this->i]);

		this->attack += v;

		if ( !this->me_c().isValid() ) {
			this->attack = this->square;
			this->me().increment();
		}
	}

	bool equal(SliderAffectVec const& other) const {
		return this->attack == other.attack;
	}

	const Square dereference() const {
		return this->attack;
	}

public:
	bool isValid() const;
protected:
	const Board* board;
	Square square;
	Man man;
	Square attack;
	Int i;

};

#endif /* SLIDERAFFECTVECABSTRACT_H_ */
